﻿#ifndef MEMSDATASAVER_H
#define MEMSDATASAVER_H

#include <qobject.h>

#include <qbytearray.h>
#include <qhash.h>
#include <qlist.h>

#include "MEMSDataHandler.h"

class FileWriter;
class LogSaver;

class MEMSDataSaver : public QObject
{
  Q_OBJECT

private:
  //Acc X H8、Acc X L8、Acc Y H8、Acc Y L8、Acc Z H8、	Acc Z L8、Gyro X H8
  //Gyro X L8、Gyro Y H8、Gyro Y L8、Gyro Z H8、Gyro Z L8、	Temp H8、Temp L8
  QString title_protocol_singleTemperature="AccX\tAccY\tAccZ\tGyroX\tGyroY\tGyroZ\tTemp";
  //Acc X H8、Acc X L8、Acc Y H8、Acc Y L8、Acc Z H8、Acc Z L8、Gyro X H8
  //Gyro X L8、Gyro Y H8、Gyro Y L8、Gyro Z H8、Gyro Z L8、Temp_ACC、Temp_GYR
  QString title_protocol_doubleTemperature= "AccX\tAccY\tAccZ\tGyroX\tGyroY\tGyroZ\tTemp_Acc\tTemp_Gyr";
  QString newLine = "\r\n";
private:
  QHash<int,QString> filePaths;//用于保存当前数据保存文件路径
  QHash<int, bool> firstWrite;//用于记录是否首次写入
  int dataGroupCount = 6;
  int accuracy = 4;

  FileWriter* fileWriter= Q_NULLPTR;
  MEMSDataHandler* mEMSDataHandler = Q_NULLPTR;
  LogSaver* logSaver= Q_NULLPTR;
public:
  MEMSDataSaver(QObject* parent = Q_NULLPTR);
  ~MEMSDataSaver();
signals:
  void signal_saveDataFileSuccessful(QString filePath);
  void signal_saveDataFileFailed(QString filePath);
  void signal_mEMSDataToSave(int dataGroupIndex,QString data);
public slots:
  void slot_saveData(int dataGroupIndex, QVector<float> data, MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol);

private:
  void writeToFile_title(int dataGroupIndex,MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol);
  void writeToFile_data(int dataGroupIndex,QVector<float> data,MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol);
  bool isFirstWrite(int dataGroupIndex);
  void setFirstWrite(int dataGroupIndex,bool firstWriteState);

  //geter and seter
public:
  QList<QString> getFilePaths();
  QString getFilePath(int dataGroupIndex);
  void setFilePath(int dataGroupIndex,QString filePath);
  int getDataGroupCount();
  void setDataGroupCount(int dataGroupCount);
  int getAccuracy();
  void setAccuracy(int accuracy);
  FileWriter* getFileWriter();
  void setFileWriter(FileWriter* fileWriter);
  MEMSDataHandler* getMEMSDataHandler();
  void setMEMSDataHandler(MEMSDataHandler* mEMSDataHandler);
  LogSaver* getLogSaver();
  void setLogSaver(LogSaver* logSaver);
};
#endif
